package ink.lovejinhu.leetcode;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-07-06 17:38
 */
public class Solution_0205 {
    /**
     * 同构字符串
     * 比如:"add“-》”egg“
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length())
            return false;
        HashMap<Character, Integer> v1 = new HashMap<>();   //存S的各个字符
        HashMap<Character, Integer> v2 = new HashMap<>();  //存t的各个字符0
        char[] chars = s.toCharArray();
        char[] chart = t.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            if (v1.containsKey(chars[i])) {   //v1包含
                if (v2.containsKey(chart[i])) {  //v2包含
                    if (v1.get(chars[i]) == v2.get(chart[i])) {    //之前的索引还相同
                        v1.put(chars[i], i);
                        v2.put(chart[i], i);
                    } else {
                        return false;
                    }
                } else {     //v2包含
                    return false;
                }
            } else {   //v1不包含
                if (v2.containsKey(chart[i])) {  //v2包含
                    return false;
                } else {  //v2也不包含
                    v1.put(chars[i], i);
                    v2.put(chart[i], i);
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Solution_0205 solution_0205 = new Solution_0205();
        String s = "paper";
        String t = "title";
        System.out.println(solution_0205.isIsomorphic(s, t));
    }
}
